import React, { Component } from "react"
import { HashRouter, Redirect, Route, Switch } from 'react-router-dom'
import { connect } from "react-redux"
import Cookies from "js-cookie"

import {autoLogin} from "../../redux/actions"
import Main from "../main/main"
import Editor from "../editor/editor"
import CardDetail from "../card-detail/card-detail"


class MainNav extends Component {

  constructor(props) {
    super(props);
    //拿cookies自动登陆
    var token=Cookies.get("token");
    if(token){
      this.props.autoLogin(token)
    }
  }


  render() {
    //做登陆拦截,如果没有登陆,自动跳转
    console.log("登陆拦截")
    var {id} =this.props.user
    if(!id){
      return <Redirect to="/login"></Redirect>
    }

    return (
        <HashRouter>
          <Switch>
            <Route path='/edit' component={Editor}></Route>
            <Route path='/detail/:id' component={CardDetail}></Route>
            <Route component={Main}></Route>
          </Switch>
        </HashRouter>
    )
  }
}

export default connect(state => ({ user: state.user }),{autoLogin})(MainNav)
